﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using System;
using System.IO;
using System.Linq;
using UnityEngine;

namespace LS.Editor.DataTableTools
{
    public sealed partial class DataTableProcessor
    {
        private sealed class Vector3Processor : GenericDataProcessor<Vector3>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Vector3";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "vector3",
                    "unityengine.vector3"
                };
            }

            public override Vector3 Parse(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector3(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                Vector3 vector3 = Parse(value);
                binaryWriter.Write(vector3.x);
                binaryWriter.Write(vector3.y);
                binaryWriter.Write(vector3.z);
            }
        }
        private sealed class Vector3ArrayProcessor : GenericDataArrayProcessor<Vector3>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Vector3[]";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "vector3[]",
                    "unityengine.vector3[]"
                };
            }

            public override Vector3 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector3(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    Vector3 vector3 = array[i];
                    binaryWriter.Write(vector3.x);
                    binaryWriter.Write(vector3.y);
                    binaryWriter.Write(vector3.z);
                }
            }
        }
        private sealed class Vector3ListProcessor : GenericDataListProcessor<Vector3>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "List<Vector3>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "list<vector3>",
                    "list<unityengine.vector3>",
                    "system.collections.generic.list<vector3>",
                    "system.collections.generic.list<unityengine.vector3>",
                };
            }

            public override Vector3 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector3(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var array = Parse(value);
                binaryWriter.Write(array.Count);
                for (int i = 0; i < array.Count; i++)
                {
                    Vector3 vector3 = array[i];
                    binaryWriter.Write(vector3.x);
                    binaryWriter.Write(vector3.y);
                    binaryWriter.Write(vector3.z);
                }
            }
        }
        private sealed class Vector3DictionaryProcessor : GenericDataDictionaryProcessor<Vector3>
        {
            public override bool IsSystem
            {
                get
                {
                    return false;
                }
            }

            public override string LanguageKeyword
            {
                get
                {
                    return "Dictionary<string,Vector3>";
                }
            }

            public override string[] GetTypeStrings()
            {
                return new string[]
                {
                    "dictionary<string,vector3>",
                    "dictionary<string,unityengine.vector3>",
                    "dictionary<system.string,Vector3>",
                    "dictionary<system.string,unityengine.vector3>",
                    "system.collections.generic.dictionary<string,vector3>",
                    "system.collections.generic.dictionary<string,unityengine.vector3",
                    "system.collections.generic.dictionary<system.string,vector3>",
                    "system.collections.generic.dictionary<system.string,unityengine.vector3>",
                };
            }

            public override Vector3 ParseItem(string value)
            {
                if (string.IsNullOrEmpty(value))
                {
                    return default;
                }
                string[] splitedValue = value.Split(',');
                return new Vector3(float.Parse(splitedValue[0]), float.Parse(splitedValue[1]), float.Parse(splitedValue[2]));
            }

            public override void WriteToStream(DataTableProcessor dataTableProcessor, BinaryWriter binaryWriter, string value)
            {
                var dic = Parse(value);
                binaryWriter.Write(dic.Count);
                foreach (var item in dic)
                {
                    binaryWriter.Write(item.Key);
                    Vector3 vector3 = item.Value;
                    binaryWriter.Write(vector3.x);
                    binaryWriter.Write(vector3.y);
                    binaryWriter.Write(vector3.z);
                }
            }
        }
    }
}
